﻿#pragma execution_character_set("utf-8")
// 本文件为utf-8 编码格式

#include "GumboUtil.h"
#include "../Util/GDUtil.h"


void GumboUtil::test(){
	std::string url = "";
	std::string html = CCFileUtils::sharedFileUtils()->getFileString("data/goodsgrap.html");
	// const string html("<html><head><title>实得分来看望</title></head><body>事发的<img src=\"http://www.baidu.com\"/>了枯萎<img src=\"http://www.baidu1.com\"/>枯萎<img src=\"http://www.baidu3.com\">sdf</body></html>");
	
	std::string title = GumboUtil::getTitle(html);
	CCLog("title:%s", title.c_str());

	/*
	vector<string> imgs = GumboUtil::getImages(html);
	if (imgs.size() > 0){
		for (int i = 0; i < imgs.size(); i++){
			CCLog("image %d:%s", i, imgs.at(i).c_str());
		}
	}
	else{
		CCLog("image is null");
	}
	*/

	std::string price = "";
	GdBean bean;
	GumboUtil::getPrice(url, html, bean);
	price = bean.value;
	CCLog("price:%s", price.c_str());
}

std::string GumboUtil::getBackText(const std::string& content, std::string tag, bool allNumber){
	//CCLog("获取指定标签： %s后面的文本内容",tag.c_str());
	if (content.empty() || content.length() < 1 || tag.empty() || tag.length()<1){
		return "";
	}
	string htmls(content);
	
	int iPos = -1;

	// 分析
	iPos = htmls.find(tag);
	if (iPos >= 0){
		//CCLog("标签： %s位置：%d", tag.c_str(), iPos);

		htmls = htmls.substr(iPos + tag.length()); // 从tag后开始
		iPos = htmls.find("</"); // 查询截取后的第一个开始标签

		if (iPos == 0){ // 如果标签后是结束标签
			//CCLog("结束了。。。");
			iPos = htmls.find(">"); // 查询结束标签位置
			htmls = htmls.substr(iPos + 1); // 从tag后开始
			iPos = htmls.find("</");
		}

		if (iPos >= 0){
			htmls = htmls.substr(0, iPos);
			CCLog("解析标签内容为：%s",htmls.c_str());
		}
		iPos = htmls.find(">"); // 查询截取后的第一个结束标签
		if (iPos >= 0){
			htmls = htmls.substr(iPos + 1);
			CCLog("解析标签内容为：%s", htmls.c_str());
		}

		GDUtil::trimNull(htmls); // 删除空格

		if (allNumber){ // 如果抓取的是全数字
			if (strspn(htmls.c_str(), "0123456789.,") == strlen(htmls.c_str())){
				return htmls;
			}
		}
		
		
	}
	else{
		//CCLog("标签： %s没有找到", tag.c_str());
	}

	return "";
}

void GumboUtil::getPrice(const std::string& sourceUrl, std::string& content, GdBean& priceBean){
	if (GDUtil::beginWith(sourceUrl,"http://item.m.yhd.com/")){ // 如果是一号店
		GumboUtil::getYhdPrice(sourceUrl, content, priceBean);
	}
	else if (GDUtil::beginWith(sourceUrl, "http://www.amazon.com/")){ // 如果是亚马逊美国
		GumboUtil::getAmazonComPrice(sourceUrl, content, priceBean);
	}
	else if (GDUtil::beginWith(sourceUrl, "http://www.amazon.cn/")){ // 如果是亚马逊中国
		GumboUtil::getAmazonCnPrice(sourceUrl, content, priceBean);
	}
	
	std::string price=""; 

	//CCLog("开始解析价格");
	priceBean.name = "￥"; // 默认价格符合
	price = GumboUtil::getBackText(content, "&yen;", true);
	CCLog("价格是:%s", price.c_str());
	if (price.empty() || price.length() < 1){
		price = GumboUtil::getBackText(content, "¥", true);
	}
	CCLog("价格是:%s", price.c_str());
	if (price.empty() || price.length() < 1){
		price = GumboUtil::getBackText(content, "￥", true);
	}
	CCLog("价格是:%s", price.c_str());

	if (!price.empty() && price.length() >= 1){
		GDUtil::replace(price, ",", ""); // 替换价格里面的分割逗号
		priceBean.value = price;
	}
	
}

std::string GumboUtil::getTitle(const std::string& content){
	string title = GumboUtil::getTagText(content, "title");
	if (title.empty() || title == ""){
		title = GumboUtil::getTagText(content, "TITLE");
	}
	/**/
	if (!title.empty() && title != ""){
		GDUtil::trimTabEnter(title);
	}

	CCLog("抓取的title:%s",title.c_str());
	return title;
}

vector<std::string> GumboUtil::getImages(const std::string& content){
	vector<std::string> imgs;

	string htmls(content);
	string imgTag = "";
	string imgUrl = "";
	int iPos = htmls.find("<img");
	int imgTagEndPos = -1;

	while (iPos >= 0)
	{
		htmls = htmls.substr(iPos + 4); // 从img开始
		imgTagEndPos = htmls.find("<"); // 查找下一个标签开始符号
		
		if (imgTagEndPos > 0){
			imgTag = htmls.substr(0, imgTagEndPos);
			htmls = htmls.substr(imgTagEndPos + 2);
			iPos = htmls.find("<img");
			imgUrl = GumboUtil::getImgSrc(imgTag); // 图片src属性
			if (!imgUrl.empty() && imgUrl != ""){
				imgs.push_back(imgUrl);
			}
		}
		else{
			iPos = -1;
		}
	}

	// 查找大写的image标签
	htmls = content;
	iPos = htmls.find("<IMG");
	imgTagEndPos = -1;

	while (iPos >= 0)
	{
		htmls = htmls.substr(iPos + 4); // 从img开始
		imgTagEndPos = htmls.find("<"); // 查找下一个标签开始符号

		if (imgTagEndPos > 0){
			imgTag = htmls.substr(0, imgTagEndPos);
			htmls = htmls.substr(imgTagEndPos + 2);
			iPos = htmls.find("<IMG");
			imgUrl = GumboUtil::getImgSrc(imgTag); // 图片src属性
			if (!imgUrl.empty() && imgUrl != ""){
				imgs.push_back(imgUrl);
			}
		}
		else{
			iPos = -1;
		}
	}

	return imgs;
}

std::string GumboUtil::getTagText(const std::string& content, const std::string tag){
	string htmls(content);
	int position = htmls.find("<" + tag );
	if (position > 0){ // 找到标题
		htmls = htmls.substr(position + tag.length()+1);

		position = htmls.find(">"); // 获取结束标签
		if (position >= 0){ // 找到了结束标签
			CCLog("找到结束标签");
			htmls = htmls.substr(position + 1);
		}

		position = htmls.find("</" + tag + ">"); // 获取结束标签
		if (position > 0){
			htmls = htmls.substr(0, position);
			return htmls;
		}
	}

	return "";
}

std::string GumboUtil::getImgSrc(const std::string& content){
	if (content.empty() || content == ""){
		return "";
	}
	string htmls(content);
	string imgUrl = "";

	GDUtil::trimNull(htmls);

	int position = htmls.find("src=\"");
	int endPos = -1;
	if (position >= 0){
		htmls = htmls.substr(position + 5);
		endPos = htmls.find("\"");
		if (endPos > 0){
			imgUrl = htmls.substr(0, endPos);
		}
	}
	else{
		position = htmls.find("SRC=\"");
		if (position >= 0){
			htmls = htmls.substr(position + 5);
			endPos = htmls.find("\"");
			if (endPos > 0){
				imgUrl = htmls.substr(0, endPos);
			}
		}
		else{
			position = htmls.find("src='");
			if (position >= 0){
				htmls = htmls.substr(position + 5);
				endPos = htmls.find("\"");
				if (endPos > 0){
					imgUrl = htmls.substr(0, endPos);
				}
			}
			else{
				position = htmls.find("SRC='");
				if (position >= 0){
					htmls = htmls.substr(position + 5);
					endPos = htmls.find("\"");
					if (endPos > 0){
						imgUrl = htmls.substr(0, endPos);
					}
				}
			}
		}
	}

	return imgUrl;
}

void GumboUtil::getYhdPrice(const std::string& sourceUrl, std::string& content, GdBean& priceBean){
	priceBean.name =  GumboUtil::getBackText(content, "pd_product-price-yen");
	 
	priceBean.value = GumboUtil::getBackText(content, "pd_product-price-num", true);

	// 价格小数
	std::string priceDecimal = GumboUtil::getBackText(content, "pd_product-price-decimal", true);
	
	if (!priceDecimal.empty() && priceDecimal != ""&&priceDecimal.length() > 0){
		GDUtil::trimNull(priceDecimal);
		priceBean.value = priceBean.value + priceDecimal;
	}
	CCLog("抓取一号店价格单位：%s,价格：%s", priceBean.name.c_str(), priceBean.value.c_str());
}

void GumboUtil::getAmazonComPrice(const std::string& sourceUrl, std::string& content, GdBean& priceBean){
	string htmls(content);
	priceBean.name = "$"; // 默认价格符合

	int position = htmls.find("id=\"priceblock_ourprice\""); // 如果有现价标签
	if (position > 0){
		htmls = htmls.substr(position);

		priceBean.value = GumboUtil::getBackText(htmls, "$", true);
	}

	htmls = content;
	position = htmls.find("id=\"price\"");
	if (position > 0){
		htmls = htmls.substr(position);
	}

	priceBean.strId = GumboUtil::getBackText(htmls, "$", true);
	if (priceBean.value.empty() || priceBean.value == "" || priceBean.value.length()==0){
		priceBean.value = priceBean.strId;
	}

	CCLog("价格是:%s，原价是:%s", priceBean.value.c_str(), priceBean.strId.c_str());
}

void GumboUtil::getAmazonCnPrice(const std::string& sourceUrl, std::string& content, GdBean& priceBean)
{
	string htmls(content);
	priceBean.name = "￥"; // 默认价格符合

	int position = htmls.find("class=\"dpOurPrice\""); // 如果有现价标签
	if (position > 0){
		htmls = htmls.substr(position);

		priceBean.value = GumboUtil::getBackText(htmls, "￥", true);
	}
	
	htmls = content;
	position = htmls.find("class=\"dpPrice\"");
	if (position > 0){
		htmls = htmls.substr(position);
	}

	priceBean.strId = GumboUtil::getBackText(htmls, "￥", true);
	if (priceBean.value.empty() || priceBean.value == "" || priceBean.value.length() == 0){
		priceBean.value = priceBean.strId;
	}

	CCLog("价格是:%s，原价是:%s", priceBean.value.c_str(), priceBean.strId.c_str());
}